﻿using System;
using System.Data;
using System.Web;
using System.Web.UI.WebControls;
using ASPNETVN.PORTAL.Components.Helpers;
using ASPNETVN.PORTAL.Components.Security;
using ASPNETVN.PORTAL.Components.UI;
using ASPNETVN.PORTAL.Modules.GUIDE.Components;

namespace ASPNETVN.PORTAL.Modules.GUIDE.Controls
{
    public partial class GuiderManager : AddEditItemControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ViewState["GuiderID"] = "";
                ViewState["dtGuider"] = null;
                LoadData();
            }
        }

        public void LoadData()
        {
            DataTable dt = Components.Guider.SelectAll(this.ModuleID).Tables[0];
            gvGuider.DataSource = dt;
            gvGuider.DataBind();
            ViewState["dtGuider"] = dt;
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Components.Guider guider = new Guider();
            guider.Title = txtTitle.Text;
            guider.Description = txtDes.Text;
            guider.ModuleID = this.ModuleID;
            //if (Int32.Parse(ViewState["GuiderID"].ToString()) != 0)
            //{
            //    guider.ID = Int32.Parse(ViewState["GuiderID"].ToString());
            //    guider.Update();
            //    ViewState["GuiderID"] = "";
            //}
            //else
            //{
                guider.Insert();
            //}
            LoadData();
            Reset();
        }        

        protected void gvGuider_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnUpdate.Visible= true;
            btnAdd.Visible = false;
            string key =gvGuider.SelectedDataKey.Value.ToString();
            ViewState["GuiderID"] = key;
            if (ViewState["dtGuider"] != null)
            {
                DataTable dt = (DataTable)ViewState["dtGuider"];
                DataRow[] dr = dt.Select("ID= " + key);
                txtTitle.Text = dr[0]["Title"].ToString();
                txtDes.Text = dr[0]["Description"].ToString();
                
            }
        }

        protected void gvGuider_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            Components.Guider.DeleteGuider(Convert.ToInt32(gvGuider.DataKeys[e.RowIndex].Value));
            LoadData();
        }

        protected void gvGuider_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvGuider.PageIndex = e.NewPageIndex;
            LoadData();
        }
        public void Reset()
        {
            txtTitle.Text = "";
            txtDes.Text = "";
            ViewState["GuiderID"] = "";
            gvGuider.SelectedIndex = -1;
            btnUpdate.Visible = false;
            btnAdd.Visible = true;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Components.Guider guider = new Guider();
            guider.Title = txtTitle.Text;
            guider.Description = txtDes.Text;
            guider.ModuleID = this.ModuleID;
            guider.ID = Int32.Parse(ViewState["GuiderID"].ToString());
            guider.Update();
            LoadData();
            Reset();
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {            
           Reset();
        }
       
    }  
}